import rclpy
from rclpy.node import Node
from sys_status.msg import SystemStatus
import psutil
import platform


class SystemStatusPub(Node):
    def __init__(self, name):
        super().__init__(name)
        self._publisher = self.create_publisher(SystemStatus, "system_status", 10)
        self._timer = self.create_timer(2, self.timer_callback)

    def timer_callback(self):
        msg = SystemStatus()
        msg.stamp = self.get_clock().now().to_msg()
        msg.host_name = platform.node()
        msg.cpu_percent = psutil.cpu_percent()
        memory_info = psutil.virtual_memory()
        msg.memory_percent = memory_info.percent
        msg.memory_total = memory_info.total / 1024 / 1024
        msg.memory_available = memory_info.available / 1024 / 1024
        net_io_info = psutil.net_io_counters()
        msg.net_sent = net_io_info.bytes_sent / 1024 / 1024
        msg.net_recieve = net_io_info.bytes_recv / 1024 / 1024
        self.get_logger().info(f"sys info:{str(msg)}")
        self._publisher.publish(msg)


def main():
    rclpy.init()
    node = SystemStatusPub("system_status_pub")
    rclpy.spin(node)
    rclpy.shutdown()
